﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Diagnostics;

namespace TFSWITConnector
{
    public class RibbonCustomControls : SPLinkButton   //WebControl
    {
        protected override void OnLoad(EventArgs e)
        {
            // Set the default button text
            //this.Text = "ValidateTFSConnection";

            // Style the button like the rest of the buttons on the toolbar
            //this.CssClass = "ms-toolbar";

            //base.OnLoad(e);

            var spWeb = SPControl.GetContextSite(Context).RootWeb;
            SPPropertyBag spProperties = spWeb.Properties;

            string __EVENTTARGET = this.Page.Request["__EVENTTARGET"];
            string __EVENTARGUMENT = this.Page.Request["__EVENTARGUMENT"];

            if (__EVENTTARGET == "tfswitconngroup.ValidateTFSConnection")
                ValidateTFSConnection(spProperties);
            else if (__EVENTTARGET == "tfswitconngroup.CreateCustomLobSystem")
                CreateCustomLobSystem(spProperties);
        }

        void ValidateTFSConnection(SPPropertyBag spProperties)
        {
            if (ValidateTFS(spProperties))
            {
                ShowMessage("TFS connection is OK!");
            }
        }

        void CreateCustomLobSystem(SPPropertyBag spProperties)
        {
            if (ValidateTFS(spProperties))
            {
                var spSite = SPControl.GetContextSite(Context);

                var lobsystemdata = new TFSSPConnector.CustomLobSystemData
                {
                    Attachment_EntityName = GetProperty(spProperties, "Attachment_EntityName"),
                    Attachment_ShowInItemView = GetProperty(spProperties, "Attachment_ShowInItemView"),
                    Attachment_ShowInListView = GetProperty(spProperties, "Attachment_ShowInListView"),
                    LobSystem_Name = GetProperty(spProperties, "LobSystem_Name"),
                    LobSystem_SPSiteURL = spSite.Url,
                    LobSystem_TFSProjectName = GetProperty(spProperties, "LobSystem_TFSProjectName"),
                    LobSystem_TFSServerURL = GetProperty(spProperties, "LobSystem_TFSServerURL"),
                    WorkItem_ReadOnlyFields = GetProperty(spProperties, "WorkItem_ReadOnlyFields"),
                    WorkItem_RequiredInForms = GetProperty(spProperties, "WorkItem_RequiredInForms"),
                    WorkItem_ShowInItemView = GetProperty(spProperties, "WorkItem_ShowInItemView"),
                    WorkItem_ShowInListView = GetProperty(spProperties, "WorkItem_ShowInListView")
                };

                if (!lobsystemdata.ValidateFields())
                {
                    ShowMessage("Some of LobSystem properties are undefined");
                    return;
                }

                TFSSPConnector.CustomLobSystem.CreateCustomLobSystem(lobsystemdata);

                ShowMessage("Creation of LobSystem structure is finished");
            }
        }

        string GetProperty(SPPropertyBag spProperties, string name)
        {
            if (!spProperties.ContainsKey(name))
            {
                ShowMessage(name+" is undefined");
                return null;
            }

            string value = spProperties[name];
            if (value == null)
            {
                return "";
            }

            return value;
        }

        bool ValidateTFS(SPPropertyBag spProperties)
        {
            if (!spProperties.ContainsKey("LobSystem_TFSServerURL"))
            {
                ShowMessage("TFS Connection URL is undefined");
                return false;
            }

            string LobSystem_TFSServerURL = spProperties["LobSystem_TFSServerURL"];
            if (string.IsNullOrEmpty(LobSystem_TFSServerURL))
            {
                ShowMessage("TFS Connection URL is undefined");
                return false;
            }

            var res = TFSSPConnector.CustomLobSystem.ValidateTFSConnection(LobSystem_TFSServerURL);
            if (!res)
            {
                ShowMessage("Cannot connect to TFS");
                return false;
            }

            return true;
        }

        void ShowMessage(string text)
        {
            // build up javascript to inject at the tail end of the page 
            /*StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("<script type=\"text/javascript\">");
            stringBuilder.AppendLine(string.Format("alert(\"{0}\");", text));
            stringBuilder.AppendLine("</script>");

            // add to the page 
            Page.Controls.Add(new LiteralControl(stringBuilder.ToString()));*/

            string clsNamespace = "TFSWITConnector";

            if (!EventLog.SourceExists(clsNamespace))
                EventLog.CreateEventSource(clsNamespace, "Application");

            EventLog.WriteEntry(clsNamespace, text, EventLogEntryType.Error);

        }
    }
}
